/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaAtendimento.java
 *
 * Created on 11/05/2012, 14:45:28
 */
package salutem.Telas;

import com.sun.corba.se.impl.ior.NewObjectKeyTemplateBase;
import com.sun.media.codec.audio.msadpcm.MsAdpcm;
import java.awt.Component;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.table.DefaultTableModel;
import salutem.Beans.AtendimentoBean;
import salutem.Beans.PacienteBean;
import salutem.Beans.PreAtendimentoBean;
import salutem.DAO.AtendimentoDAO;
import salutem.DAO.PreAtendimentoDAO;
import salutem.DAO.pacienteDAO;
import salutem.Utils.Msg;
import salutem.Utils.Params;
import salutem.Utils.Utils;

/**
 *
 * @author Tironi
 */
public class TelaAtendimento extends javax.swing.JDialog {

    private Integer idPaciente;
    private Integer idAtendimento;
    private Integer idPreAtendimento;
    private int idPac;
    private int temperatura;
    private int alta;
    private int baixa;
    private pacienteDAO pacientedao = new pacienteDAO();
    private boolean inserir;
    private AtendimentoDAO atendimentoDao;
    private PreAtendimentoDAO preDao;
    private int row;
    private TelaPrincipal principal;
    private String nomeMedico;
    private TelaPacientePesquisa telaBusca;

    /** Creates new form TelaAtendimento */
    public TelaAtendimento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        dtAtendimento.enable(false);
        this.principal = (TelaPrincipal) parent;
        this.atendimentoDao = new AtendimentoDAO();
        this.preDao = new PreAtendimentoDAO();
        this.nomeMedico = this.principal.getUsuario().getFuncionario().getNome();
        this.lbUsuario.setText(this.nomeMedico);

        Utils.maximizar(this);
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnPesquisaPaciente = new javax.swing.JButton();
        lbNome = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lbDataNascimento = new javax.swing.JLabel();
        lbIdade = new javax.swing.JLabel();
        lbArterial = new javax.swing.JLabel();
        lbTemperatura = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txAlta = new javax.swing.JLabel();
        txBaixa = new javax.swing.JLabel();
        txTemp = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        txDescricaoHis = new javax.swing.JTextArea();
        jScrollPane5 = new javax.swing.JScrollPane();
        txMedicamentosHis = new javax.swing.JTextArea();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txDiagnostico = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txDescricao = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        dtAtendimento = new org.jdesktop.swingx.JXDatePicker();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txMedicamento = new javax.swing.JTextArea();
        lbUsuario = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Paciente"));

        jLabel1.setText("Nome:");

        btnPesquisaPaciente.setText("Localizar Paciente");
        btnPesquisaPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisaPacienteActionPerformed(evt);
            }
        });

        jLabel2.setText("Data de Nascimento:");

        jLabel3.setText("Idade:");

        jLabel4.setText("Pressão Arterial");

        jLabel5.setText("Temperatura:");

        jLabel10.setText("Alta:");

        jLabel11.setText("Baixa:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 545, Short.MAX_VALUE)
                        .addComponent(btnPesquisaPaciente)
                        .addGap(144, 144, 144))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbDataNascimento))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbIdade))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(lbArterial)
                        .addGap(90, 90, 90)
                        .addComponent(lbTemperatura))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txTemp)
                        .addGap(128, 128, 128)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txAlta)
                                .addGap(26, 26, 26)
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txBaixa)))
                        .addGap(556, 556, 556))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbNome)
                    .addComponent(btnPesquisaPaciente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbDataNascimento))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lbIdade))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txTemp))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 5, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(jLabel10)
                            .addComponent(txAlta)
                            .addComponent(txBaixa))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbArterial)
                    .addComponent(lbTemperatura))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Histórico Recente"));

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Data", "Diagnóstico", "Temperatura", "PA Alta", "PA Baixa", "Médico"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pegaId(evt);
            }
        });
        tabela.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                navegarEntreTabela(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);

        txDescricaoHis.setColumns(20);
        txDescricaoHis.setEditable(false);
        txDescricaoHis.setRows(5);
        jScrollPane4.setViewportView(txDescricaoHis);

        txMedicamentosHis.setColumns(20);
        txMedicamentosHis.setEditable(false);
        txMedicamentosHis.setRows(5);
        jScrollPane5.setViewportView(txMedicamentosHis);

        jLabel12.setText("Histórico Descrição");

        jLabel13.setText("Histórico Medicamentos");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 441, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addGap(257, 257, 257))
                    .addComponent(jLabel13))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados de Atendimento"));

        jLabel6.setText("Diagnóstico");

        jLabel7.setText("Descrição");

        txDescricao.setColumns(20);
        txDescricao.setRows(5);
        jScrollPane2.setViewportView(txDescricao);

        jLabel8.setText("Data Atendimento");

        dtAtendimento.setEditable(false);

        jLabel9.setText("Medicamento");

        txMedicamento.setColumns(20);
        txMedicamento.setRows(5);
        jScrollPane3.setViewportView(txMedicamento);

        jLabel14.setText("Dr(a):");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(txDiagnostico, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbUsuario)))
                .addGap(36, 36, 36)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(dtAtendimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(77, 77, 77))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addContainerGap(389, Short.MAX_VALUE))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(11, 11, 11)
                                .addComponent(txDiagnostico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addComponent(jLabel8)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(dtAtendimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel6)))
                .addContainerGap(22, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(68, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(lbUsuario))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton2.setText("Gerar Receita");

        jButton3.setText("Sair");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Salvar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jButton4)
                    .addComponent(jButton3))
                .addContainerGap(12, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(159, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnPesquisaPacienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisaPacienteActionPerformed
    TelaPacientePesquisa telaBusca = new TelaPacientePesquisa(this, true);
    telaBusca.setVisible(true);
}//GEN-LAST:event_btnPesquisaPacienteActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    setInserir(true);
    salvar();
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    cancelar();
}//GEN-LAST:event_jButton3ActionPerformed

private void pegaId(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pegaId
  try {
        pegarId();
    } catch (SQLException ex) {
        Logger.getLogger(TelaAtendimento.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_pegaId

private void navegarEntreTabela(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_navegarEntreTabela
  try {
        pegarId();
    } catch (SQLException ex) {
        Logger.getLogger(TelaAtendimento.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_navegarEntreTabela

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaAtendimento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaAtendimento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaAtendimento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaAtendimento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaAtendimento dialog = new TelaAtendimento(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    protected void setInserir(boolean inserir) {
        this.inserir = inserir;
        this.idPaciente = null;
    }

    protected boolean isInserir() {
        return inserir;
    }

    private void destacarCampo(Component c, boolean b) {
        if (b) {
            c.setBackground(Params.COR_CAMPO_VAZIO);
        } else {
            c.setBackground(Params.COR_CAMPO_NORMAL);
        }
    }

    private boolean verificarCampos() {
        boolean aux = false;
        String msg = "Preencha corretamente os campos. \n";



        if (this.txDiagnostico.getText().isEmpty()) {
            aux = true;
            this.destacarCampo(this.txDiagnostico, aux);

        }

        if (this.txDescricao.getText().isEmpty()) {
            aux = true;
            this.destacarCampo(this.txDescricao, aux);

        }

        if (this.txMedicamento.getText().isEmpty()) {
            aux = true;
            this.destacarCampo(this.txMedicamento, aux);

        }


        if (aux) {
            Msg.alerta(this, msg);
        }
        return aux;
    }

    protected void atualizarTabela() {
        try {
            DefaultTableModel modelo = (DefaultTableModel) this.tabela.getModel();
            modelo.setNumRows(0);

            this.atendimentoDao = new AtendimentoDAO();

            List<AtendimentoBean> lista = this.atendimentoDao.getLista(this.idPac);

            for (int i = 0; i < lista.size(); i++) {
                modelo.addRow(new Object[]{
                            lista.get(i).getIdAtendimento(),
                            lista.get(i).getData(),
                            lista.get(i).getDiagnostico(),
                            lista.get(i).getTemperatura(),
                            lista.get(i).getAlta(),
                            lista.get(i).getBaixa(),
                            lista.get(i).getNomeMedico(),
                });
                            

            }


        } catch (SQLException ex) {
            Msg.erro(this, "Erro ao atualizar tabela. \n" + ex.getMessage());
        }
    }

    public void desabilitarCampos() {
        this.txDescricaoHis.enable(false);
        this.txMedicamentosHis.enable(false);
    }

    public void pegarId() throws SQLException {

        this.row = this.tabela.getSelectedRow();

        if (row == -1) {
            Msg.alerta(this, "Selecione o registro.");
            return;
        }

        DefaultTableModel modelo = (DefaultTableModel) this.tabela.getModel();
        int id = Integer.parseInt(modelo.getValueAt(this.row, 0).toString());



        this.preencheTela(id);



    }

    public void preencheTela(int id) throws SQLException {


        this.atendimentoDao = new AtendimentoDAO();
        AtendimentoBean atend = this.atendimentoDao.getAtendimento(id);
        this.idAtendimento = atend.getIdAtendimento();
        this.txMedicamentosHis.setText(atend.getMedicamento());
        this.txDescricaoHis.setText(atend.getDescricao());
    }

    protected void preencherCampos(int id) {
        try {

            this.idPac = id;



            PacienteBean paciente = pacientedao.getPaciente(id);

            Utils util = new Utils();

            this.idPaciente = paciente.getIdPaciente();
            this.lbNome.setText(paciente.getNome().trim().toUpperCase());
            this.lbDataNascimento.setText(util.convertData(paciente.getData()));



            try {
                this.preDao = new PreAtendimentoDAO();
                PreAtendimentoBean pre = preDao.getUltimoAtendimento(id);
                this.idPreAtendimento = pre.getIdPreAtendimento();
                this.txTemp.setText(String.valueOf(pre.getTemperatura()));
                this.txAlta.setText(String.valueOf(pre.getAlta()));
                this.txBaixa.setText(String.valueOf(pre.getBaixa()));

                this.temperatura = pre.getTemperatura();
                this.alta = pre.getAlta();
                this.baixa = pre.getBaixa();
            } catch (Exception e) {
                Msg.alerta(this, "Não possui Pré-Atendimento");
            }

            atualizarTabela();





        } catch (SQLException ex) {
            Msg.erro(this, "Erro ao preencher campos. \n" + ex.getMessage());
        }

    }

    private void cancelar() {

        this.setVisible(false);
        this.dispose();
    }

    private void salvar() {
        if (this.verificarCampos()) {
            return;
        }
        try {
            if (this.isInserir()) {



                AtendimentoBean atendimento = new AtendimentoBean();
                atendimento.setDiagnostico(txDiagnostico.getText().trim().toUpperCase());
                atendimento.setDescricao(txDescricao.getText().trim().toUpperCase());
                atendimento.setIdPaciente(this.idPac);
                atendimento.setData(dtAtendimento.getDate());
                atendimento.setMedicamento(txMedicamento.getText().trim().toUpperCase());
                atendimento.setTemperatura(this.temperatura);
                atendimento.setAlta(this.alta);
                atendimento.setBaixa(this.baixa);
                atendimento.setNomeMedico(this.nomeMedico);




                this.atendimentoDao = new AtendimentoDAO();
                this.atendimentoDao.inserir(atendimento);
                Msg.informacao(this, "Salvo com sucesso.");
                atualizarTabela();
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
            Msg.erro(this, "Erro ao salvar. \n" + ex.getMessage());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPesquisaPaciente;
    private org.jdesktop.swingx.JXDatePicker dtAtendimento;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JLabel lbArterial;
    private javax.swing.JLabel lbDataNascimento;
    private javax.swing.JLabel lbIdade;
    private javax.swing.JLabel lbNome;
    private javax.swing.JLabel lbTemperatura;
    private javax.swing.JLabel lbUsuario;
    private javax.swing.JTable tabela;
    private javax.swing.JLabel txAlta;
    private javax.swing.JLabel txBaixa;
    private javax.swing.JTextArea txDescricao;
    private javax.swing.JTextArea txDescricaoHis;
    private javax.swing.JTextField txDiagnostico;
    private javax.swing.JTextArea txMedicamento;
    private javax.swing.JTextArea txMedicamentosHis;
    private javax.swing.JLabel txTemp;
    // End of variables declaration//GEN-END:variables
}
